/**
 * fileName:  ArgumentCostBean.java
 * author:  Pitaya
 * Company:  Cloudland Studio
 * Createdate:  2014-6-7 上午12:42:31
 *
 * All rights Reserved, Designed By cloudland Copyright(C) 2010-2011
 *   
 * Modification  History:
 *     Date           Author        Version        
 * -------------------------------------------
 * 2014-6-7            Pitaya            1.0        
 *
 * Why & What is modified:
 * 1.0: 文件创建
 */
package org.cloudland.commercial.masterdata.handler.support;

import java.util.ArrayList;
import java.util.List;

import org.cloudland.commercial.masterdata.bean.ArgumentCostInfo;
import org.cloudland.commercial.masterdata.bean.RelationCost;
import org.cloudland.commercial.masterdata.store.entity.CostEntity;
import org.cloudland.commercial.masterdata.store.read.ArgumentCostReader;
import org.cloudland.commercial.masterdata.store.write.ArgumentCostWriter;

 /**
 * <pre> 
 * TODO 请充分描述该类提供的作用, 或与其他类会有关联的使用。
 * 使用 java.lang.Object#toString(), 或者 {@link java.lang.Character Character} 等标签
 * </pre>
 * @ClassName  ArgumentCostBean
 * @author  Pitaya
 * @version  %I%, %G%
 * @see  
 * @since   JDK1.6
 *
 */
public class ArgumentCostBean implements ArgumentCostInfo, RelationCost {
	
	private CostEntity costEntity;
	
	private List<ArgumentCostInfo> childCostInfo = new ArrayList<ArgumentCostInfo>();
	
	/**
	 * costWriter 增、删、改数据库操作
	 */
	private ArgumentCostWriter costWriter;
	
	/**
	 * costReader 查询数据库操作
	 */
	private ArgumentCostReader costReader;
	
	/**
	 * <pre>
	 * TODO 请充分描述该构造函数的作用，及其参数的作用。
	 * </pre>
	 *
	 * @param costEntity
	 * @param costWriter
	 * @param costReader   
	 */
	public ArgumentCostBean(CostEntity costEntity,
			ArgumentCostWriter costWriter, ArgumentCostReader costReader) {
		this.costEntity = costEntity;
		this.costWriter = costWriter;
		this.costReader = costReader;
	}

	/**
	 * <pre>
	 * TODO 请充分描述该构造函数的作用，及其参数的作用。
	 * </pre>
	 *
	 * @param costWriter
	 * @param costReader   
	 */
	public ArgumentCostBean(ArgumentCostWriter costWriter,
			ArgumentCostReader costReader) {
		this(new CostEntity(), costWriter, costReader);
	}

	/**
	 * @return
	 */
	public String getCostId() {
		return costEntity.getCostId();
	}

	/**
	 * @return
	 */
	public String getCostName() {
		return costEntity.getCostName();
	}
	
	public void setParentId(String parentId){
		costEntity.setParentCostId(parentId);
	}

	/**
	 * @return
	 */
	public ArgumentCostInfo[] getChilds() {
		List<CostEntity> costList = costReader.findChildCostById(getCostId());
		if(null == costList || costList.size() < 1)
		{
			return new ArgumentCostInfo[0];
		}
		if(costList.size() > 0)
		{
			childCostInfo = new ArrayList<ArgumentCostInfo>(costList.size());
		}
		for(CostEntity costEntity : costList)
		{
			childCostInfo.add(new ArgumentCostBean(costEntity,this.costWriter,this.costReader));
		}
		return childCostInfo.toArray(new ArgumentCostInfo[childCostInfo.size()]);
	}

	/**
	 * @param costName
	 */
	public void setCostName(String costName) {
		costEntity.setCostName(costName);
	}

	/**
	 * @param argumentCost
	 */
	public void addChildArgumentCost(ArgumentCostInfo argumentCost) {
		childCostInfo.add(argumentCost);
	}

	/**
	 * 
	 */
	public void submit() {
		if(null != costEntity.getCostId() && costEntity.getCostId().length() > 0)
		{
			this.costWriter.update(costEntity);
		}
		else
		{
			this.costWriter.save(costEntity);
		}
		
		for(ArgumentCostInfo child : childCostInfo)
		{
			((ArgumentCostBean) child).setParentId(this.getCostId());
			child.submit();
		}
	}
	
}
